package com.example.back.controller;

import com.example.back.model.PsGame;
import com.example.back.service.PsGameService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * PS游戏控制层
 */
@RestController
@RequestMapping("/psgame")
public class PsGameController {
    private static final Logger logger = LogManager.getLogger(PsGameController.class);

    @Resource
    private PsGameService psGameService;

    @RequestMapping("/getPsgameList")
    @ResponseBody
    public List<PsGame> getPsGameList(){
        List<PsGame> psGameList = psGameService.findAllPsGame();
        return psGameList;
    }

    @RequestMapping("/insertPsGame")
    public String insertPsGame(String name, String introduction, String yuyan, String type, double score) {
        String rs = "";
        if (psGameService.insertPsGame(name, introduction, yuyan, type, score)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新的PS游戏添加成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新的PS游戏添加失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/updatePsGame")
    public String updatePsGame(int id, String yuyan, double score) {
        logger.info("yuyan: " + yuyan + ";score: " + score);
        String rs = "";
        if (yuyan != null && yuyan.length() != 0 && score != 0) {
            if (psGameService.updateYuyanByID(id, yuyan) && psGameService.updateScoreByID(id, score)) {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>PS游戏的语言版本和评分更新成功！点击返回（3秒后自动返回）</button>";
            } else {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>PS游戏的语言版本和评分未完全更新！点击返回（3秒后自动返回）</button>";
            }
        } else if (yuyan != null && yuyan.length() != 0) {
            if (psGameService.updateYuyanByID(id, yuyan)) {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>PS游戏的语言版本更新成功！点击返回（3秒后自动返回）</button>";
            } else {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>PS游戏的语言版本更新失败！点击返回（3秒后自动返回）</button>";
            }
        } else if (score != 0) {
            if (psGameService.updateScoreByID(id, score)) {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>PS游戏的评分更新成功！点击返回（3秒后自动返回）</button>";
            } else {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>PS游戏的评分更新失败！点击返回（3秒后自动返回）</button>";
            }
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>PS游戏的语言版本和评分输入非法，请重新输入！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }
    @RequestMapping("/deletePsGame")
    public String deletePsGame(int id) {
        String rs = "";
        if (psGameService.deletePsGame(id)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除PS游戏成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除PS游戏失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }
    @RequestMapping("watchPPhoto")
    public String watchPPhoto(int id) {
        return "<img src='" + psGameService.findPhotoByID(id) + "'>";
    }
}
